﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;
using  System.Collections;
namespace MKIB3Game
{
    public class Receiver
    {
         private TcpClient client;
         private ArrayList buffer;
         public bool MAINLOOP = true;
         int TIME_OUT_BEFORE_RECEIVING = 1000;
         Connector c;

        public Receiver(TcpClient client, Connector c)
        {                
            this.client = client;
            this.c = c;
            buffer = new ArrayList();
            initThread();
                
        }
        public int getBufferSize()
        {
            return buffer.Count;
        }

        public string readBufferLine()
        {
            if (buffer.Count != 0)
            { 
                lock (buffer)
                {
                    string ret = (string)buffer[0];
                    if (ret.Equals("") || ret.Equals("\0"))
                        ret = null;
                    buffer.RemoveAt(0);             
                    return ret;
                   }

              
            }
            return null; 
        }
        
        private void  initThread()
        {
            Thread receiverThread = new Thread(receive);
            receiverThread.Name = "receiver Thread";
            receiverThread.Start();
        }
        private void receive(){ 
            Console.WriteLine(Thread.CurrentThread.Name + " ist the curret thread");
          
            try
            {
               
               
                    Console.WriteLine("receiving...");
                    NetworkStream stream = client.GetStream();
                    StreamReader sr = new StreamReader(stream);
                    Console.WriteLine(Thread.CurrentThread.Name + " wird " + TIME_OUT_BEFORE_RECEIVING + "ms angehalten");
                    Thread.Sleep(TIME_OUT_BEFORE_RECEIVING);

                    while (MAINLOOP)
                    {
                        
                            if (stream.CanRead)
                            {
    
                           
                               // byte[] bytes = new byte[client.ReceiveBufferSize];
                                String ret = sr.ReadLine();
                                lock (buffer)
                                {
                                    buffer.Add(ret);
                                }
                                Console.WriteLine("Received: [" + ret + "]");
                        }
                            
                            else
                            {
                                Console.WriteLine("reading from stream not possibile");
                                return;
                            }
                       
                        }
                    
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e + "\n");
            }
            catch (IOException)
            {
                Console.WriteLine("Verbindung zum Server verloren oder fehlgeschlagen");
                c.reconnect();
            }
            catch (Exception e)
            {
                Console.WriteLine("exception in rec");
                Console.WriteLine(e.Message + "\n");
                throw e;
            }
        }//end receive
          
    }
}
